$(function () {
    inIt()
    var layer = layui.layer;
    $('#btnLogout').on('click', function () {
        layer.confirm('确定退出登录?', { icon: 3, title: '提示' }, function (index) {
            localStorage.removeItem('token');
            location.href = '/login.html'
            layer.close(index)
        }
        )
    })
    // 发请求

    // 头像和昵称

})
function inIt() {

    $.ajax({
        url: '/my/userinfo',
        method: 'GET',
        success: function (res) {
            if (res.status === 0) {
                renderAvatar(res.data)
            } else {
                // layer.msg(res.message, { icon: 2 });
                location.href = '/login.html'
            }
        }
    })
};
function renderAvatar(user) {
    var name = user.nickname || user.username;
    name = name.replace(/\s/gi, '');
    $('.welcome-name').html(`欢迎您 ${name}`)
    $('.layui-nav-img').attr('src', user.user_pic);


    $('.my-name').text(name[0].toUpperCase())
    if (user.user_pic !== null) {
        $('.my-name').hide();
        $('.layui-nav-img').show()
    } else {
        $('.my-name').show();
        $('.layui-nav-img').hide()
    }
}